//
//  LCZFMDatabase.m
//  PersonalNews
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZFMDatabase.h"
#import "LCZLastColumnModel.h"

@implementation LCZFMDatabase

+ (instancetype)shareFMDatabase
{
    static LCZFMDatabase *lczFMDatabase;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        lczFMDatabase = [[LCZFMDatabase alloc]init];
    });
    
    NSString *doc = PATH_OF_DOCUMENT;
    NSString *document = [doc stringByAppendingString:@"/Documents"];
    //给数据库路径
    NSString *path = [document stringByAppendingString:@"LCZDataBase.sqlite"];
    lczFMDatabase.dbPath = path;
    
    return lczFMDatabase;

}
// 打开数据库
- (void)openDB:(NSInteger)ModelStyle
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 如果文件管理者没找到这个路径下的文件
    if (![fileManager fileExistsAtPath:self.dbPath]) {
        // 创建数据库
        FMDatabase *dataBase = [FMDatabase databaseWithPath:self.dbPath];
        // 如果数据库已经打开
        if ([dataBase open]) {
            switch (ModelStyle) {
                case 0:
                {
                    // 创建表
                    NSString * sql0 = @"CREATE TABLE 'LCZAVDataBase' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL , 'name' VARCHAR(30), 'sex' VARCHAR(30))";
                    // 更新 判断数据库是否有表
                    BOOL response = [dataBase executeUpdate:sql0];
                    if (!response) {
                        NSLog(@"error when creating database table");
                    } else {
                        NSLog(@"succ to creating database table");
                    }

                    break;
                }
                case 1:
                {
                    NSString *sql1 = @"CREATE TABLE 'LCZNewsDataBase' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL , 'title' VARCHAR(30), 'url' VARCHAR(30))";
                    // 更新 判断数据库是否有表
                    BOOL response = [dataBase executeUpdate:sql1];
                    if (!response) {
                        NSLog(@"error when creating database table");
                    } else {
                        NSLog(@"succ to creating database table");
                    }

                    break;
                }
                case 2:
                {
                    NSString *sql2 = @"CREATE TABLE 'LCZReadDataBase' ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL , 'title' VARCHAR(30), 'url' VARCHAR(30))";
                    // 更新 判断数据库是否有表
                    BOOL response = [dataBase executeUpdate:sql2];
                    if (!response) {
                        NSLog(@"error when creating database table");
                    } else {
                        NSLog(@"success to creating database table");
                    }

                    break;
                }
            }
            // 关闭数据库
            [dataBase close];
        } else
        {
            NSLog(@"error when open database");
        }
    }
}

// 添加数据(model 直接在里面确定了, 就不传进来了)
- (void)insertData:(id)model :(NSInteger)ModelStyle
{
    FMDatabase *dataBase = [FMDatabase databaseWithPath:self.dbPath];
    if ([dataBase open]) {
        switch (ModelStyle) {
            case 0:
            {
//                NSString *sql = @"insert into LCZAVDataBase (array, dictionary) values(?, ?) ";
//                BOOL response = [dataBase executeUpdate:sql];
//                if (!response) {
//                    NSLog(@"error when insertData");
//                } else {
//                    NSLog(@"success when insertData");
//                }

                break;
            }
            case 1:
            {
//                NSString *sql = @"insert into LCZNewsDataBase (array, dictionary) values(?, ?) ";
//                BOOL response = [dataBase executeUpdate:sql];
//                if (!response) {
//                    NSLog(@"error when insertData");
//                } else {
//                    NSLog(@"success when insertData");
//                }

                break;
            }
            case 2:
            {
                LCZLastColumnModel *lastModel = [[LCZLastColumnModel alloc]init];
                lastModel = model;
                NSString *sql = @"insert into LCZReadDataBase (title, url) values(?, ?) ";
                BOOL response = [dataBase executeUpdate:sql, lastModel.title, lastModel.url];
                if (!response) {
                    NSLog(@"error when insertData");
                } else {
                    NSLog(@"success when insertData");
                }

                break;
            }
        }
        [dataBase close];
    }
}

//  查询数据
- (NSMutableArray *)queryData:(NSInteger)ModelStyle
{
    FMDatabase *dataBase = [FMDatabase databaseWithPath:self.dbPath];
    if ([dataBase open]) {
        switch (ModelStyle) {
            case 0:
            {
                // AV的model
                NSMutableArray *arr = [NSMutableArray array];
                return arr;
                break;
            }
            case 1:
            {
                // News的model
                NSMutableArray *arr = [NSMutableArray array];
                return arr;
                break;
            }
            case 2:
            {
                // Read的model
                NSMutableArray *arr = [NSMutableArray array];
                NSString * sql = @"select * from LCZReadDataBase";
                //??????? // 查询
                FMResultSet * rs = [dataBase executeQuery:sql];
                while ([rs next]) {
                    LCZLastColumnModel *lastModel = [[LCZLastColumnModel alloc]init];
                    lastModel.title = [rs stringForColumn:@"title"];
                    lastModel.url = [rs stringForColumn:@"url"];
                    [arr addObject:lastModel];
                }
                return arr;
                break;
            }
        }
        [dataBase close];
    }
    return nil;
}

// 查询数据库是否存在
- (BOOL)selectAllByLCZColumnModelTitle:(NSString *)title
{
    BOOL score;
    FMDatabase * dataBase = [FMDatabase databaseWithPath:self.dbPath];
    if ([dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"SELECT title FROM LCZReadDataBase WHERE title = '%@'",title];
        FMResultSet *resultSet = [dataBase executeQuery:sql];
        while ([resultSet next]) {
            if ([[resultSet stringForColumn:@"title"] isEqualToString:title]) {
                score = YES;
            } else {
                score = NO;
            }
                  }
        [dataBase close];
    }
    return score;
}

// 删除数据
- (void)clearAll:(id)model
{
    LCZLastColumnModel *lastModel = [[LCZLastColumnModel alloc]init];
    lastModel = model;
    FMDatabase * dataBase = [FMDatabase databaseWithPath:self.dbPath];
    if ([dataBase open]) {
        NSString * sql = [NSString stringWithFormat:@"delete from LCZReadDataBase where title = '%@'", lastModel.title];
        // 更新
        BOOL res = [dataBase executeUpdate:sql];
        if (!res) {
            NSLog(@"error to delete database data");
        } else {
            NSLog(@"success to deleta database data");
        }
        [dataBase close];
    }

}



@end
